<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/18
 * Time: 10:43 AM
 */

include_once "../../Util/DebugUtil/DebugUtil.php";


$timeStr1 = "2014/01/02";
$timeStr2 = "2014/01-02";
$timeStr3 = "2014/1/2";
$timeStr4 = "2014/1-2";

// 时间模式
$pattern = "/^\d{4}\W\d{2}\W\d{2}$/";

DebugUtil::printDebugSeperator("preg_match");
echo "pattern = {$pattern}";
yt_debug_print_break_line();
$match = preg_match($pattern, $timeStr1);
echo " $timeStr1 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr2);
echo " $timeStr2 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr3);
echo " $timeStr3 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr4);
echo " $timeStr4 match = {$match}";
yt_debug_print_break_line();


$pattern = "/^\d{4}(\W)\d{1,2}\\1\d{1,2}$/";

DebugUtil::printDebugSeperator("preg_match 后向引用 \\1 引用 (\W)");
echo "pattern = {$pattern}";
yt_debug_print_break_line();
$match = preg_match($pattern, $timeStr1);
echo " $timeStr1 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr2);
echo " $timeStr2 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr3);
echo " $timeStr3 match = {$match}";
yt_debug_print_break_line();

$match = preg_match($pattern, $timeStr4);
echo " $timeStr4 match = {$match}";
yt_debug_print_break_line();


DebugUtil::printDebugSeperator("preg_match 匹配URL");

/*
 * (https?|ftps?) 匹配协议：http/https/ftp/ftps
 * (\w+) 匹配玉 www/blog/...
 * ([^\.\/]+) 匹配主机 不包含"."和"/"的至少有一个字符的字符串
 * (com|net|org) 后缀
 * (\/[\w-\.\/\?\%\&\=]*) path和参数
 * */
$pattern = "/(https?|ftps?):\/\/(\w+)\.([^\.\/]+)\.(com|net|org)(\/[\w-\.\/\?\%\&\=]*)?/i";
$subject="网址为http://blog.lampbrother.net/index.php&page=2的位置是LAMP兄弟连";  //被搜索字符串
if(preg_match($pattern, $subject, $matches)) {       //使用preg_match()函数进行匹配
    echo "搜索到的URL为：".$matches[0]."<br>";   //数组中第一个元素保存全部匹配结果
    echo "URL中的协议为：".$matches[1]."<br>";   //数组中第二个元素保存第一个子表达式
    echo "URL中的主机为：".$matches[2]."<br>";   //数组中第三个元素保存第二个子表达式
    echo "URL中的域名为：".$matches[3]."<br>";   //数组中第四个元素保存第三个子表达式
    echo "URL中的顶域为：".$matches[4]."<br>";   //数组中第五个元素保存第四个子表达式
    echo "URL中的文件为：".$matches[5]."<br>";   //数组中第六个元素保存第五 个子表达式
} else {
    echo "搜索失败！";            //如果和正则表达式没有匹配成功则输出此条语句
}


?>